package main

import (
	"encoding/gob"
	"exam_go/config"
	"exam_go/middleware"
	"exam_go/router"

	"github.com/gin-contrib/cors"
	"github.com/gin-contrib/sessions"
	"github.com/gin-contrib/sessions/cookie"
	"github.com/gin-gonic/gin"
)

type usersession struct {
	UserId int    `json:"userId"`
	Number string `json:"number"`
	Name   string `json:"name"`
	Status string `json:"status"`
}

func main() {
	gob.Register(usersession{})
	gin.SetMode(config.Config.AppMode)
	r := gin.Default()

	r.Use(cors.New(cors.Config{
		AllowOrigins:     []string{"http://localhost:8080", "http://localhost:5173"},              // 允许的前端地址
		AllowMethods:     []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},                     // 允许的方法
		AllowHeaders:     []string{"Origin", "Content-Type", "Authorization", "X-Requested-With"}, // 允许的请求头
		AllowCredentials: true,                                                                    // 是否允许携带凭证（如 Cookies）
	}))

	store := cookie.NewStore([]byte("snaosnca"))
	r.Use(sessions.Sessions("SESSIONID", store))

	// model.InitModel()
	config.InitSession(r)

	r.Use(middleware.Error)
	router.InitRouter(r)
	r.Run(":8088")
}
